# 导入模块
import os
import sys


# 按照分类名称，重命名数据集的文件夹
def add_prefix_subfolders(path=''):                                                    # 定义函数名称
    # mark = word                                                                      #准备添加的前缀内容
    num = 0
    old_names = os.listdir(path)                                                       # 取路径下的文件名，生成列表
    print(old_names)
    for old_name in old_names:                                                         # 遍历列表下的文件名
        if old_name != sys.argv[0]:                                 # 代码本身文件路径，防止脚本文件放在path路径下时，被一起重命名
            os.rename(os.path.join(path, old_name), os.path.join(path, str(num)))      # 子文件夹重命名mark+old_name
            print(old_name, "has been renamed successfully! New name is: ", str(num))  # mark+old_name
        num = num + 1


def change_jia_name(flie='', num='', path=''):    # 将种类名与数字进行对应
    local = flie                                  # 用于记录的文件
    # mark = word                                 # 准备添加的前缀内容
    num_name = []                                 # 用于记录数字
    old_names = os.listdir(path)                  # 取路径下的文件名，生成列表
    print(old_names)
    for i in range(num):
        num_name.append(i)
    print(num_name)
    with open(local, 'a', encoding='utf-8') as f:
        for a in range(38):
            b = str(a)
            text = b + ':' + old_names[a] + '\n'
            f.write(text)
    f.close()


if __name__ == '__main__':
    # path = r'.\dataset\train'                   #运行程序前，记得修改主文件夹路径
    # 训练集的路径，文件和编号一一对应到data.txt
    add_prefix_subfolders(r'.\dataset\train')     # 调用定义的函数
    # change_jia_name('data.txt',38,r'.\dataset\train')#将种类名与数字进行对应
